// search模块的小仓库
import {reqGetCommodityList} from '@/api'
import axios from 'axios'
const state ={
    commodityList:[],
    // 数据获取的商品信息
    goodsList1:[],
    allgoodspublist:[]
}
const mutations={
    GATCOMMODITYLIST(state,value){
        state.commodityList = value
    },
    GATALLGOODS(state,value){
        state.goodsList1 = value
    },
    GATALLGOODSPUB(state,value){
        state.allgoodspublist = value
    }
}
const actions={
    async getCommodityList({commit}){
        let result = await reqGetCommodityList(); 
        // console.log(result)
        if(result.code === 200){
            commit("GATCOMMODITYLIST",result.data)
            // console.log(result.data)
        }
    },
    // 获取数据库里的商品信息
    async getGoodsList({commit}){
        axios.get("http://localhost:8080/api/getAllGoods/getAllGoodslist",{
            params:{
              id:1
            }
          }).then(
            res=>{
              let result = res.data
              console.log("aa",result)
              if(res.status == 200){
                  commit("GATALLGOODS",result)
              }
              console.log("a",res)
  
            },
            error=>{
              console.log("b",error.message)
            }
          )
    },
    // 获取拍卖商品的时间
    getAllGoodsPublist({commit}){
        axios.get("http://localhost:8080/api/getAllGoodspub/getAllGoodspublist",{
          params:{
            id:1
          }
        }).then(
          res=>{
            let result = res.data
            if(res.status == 200){
                for(var i= 0;i<result.length;i++){
                    var startTemp = Number(String(result[i].startTime).slice(11,13))+8
                    var endTemp = Number(String(result[i].startTime).slice(11,13))+8
                    var startHour = startTemp%24 > 10? startTemp%24 :'0'+startTemp%24
                    var endHour = endTemp%24 > 10? endTemp%24 :'0'+endTemp%24
                    result[i].startTime = String(result[i].startTime).slice(0,10)+ ' ' + startHour + String(result[i].startTime).slice(13,19)
                    result[i].endTime = String(result[i].endTime).slice(0,10)+ ' ' + endHour + String(result[i].endTime).slice(13,19)
                }
                commit("GATALLGOODSPUB",result)
            }

          },
          error=>{
            console.log("88",error.message)
          }
        )
    },
}
const getters={
    getGoodsform:(state)=>(id)=>{
        var goodsfrom = []
        var allgoods = []
        for(let i in state.goodsList1){
            // console.log("相关图片",state.goodsList1[i].goodsRelateimg )
            if(!Array.isArray(state.goodsList1[i].goodsRelateimg)){
                state.goodsList1[i].goodsRelateimg = state.goodsList1[i].goodsRelateimg.split(';').slice(0,-1)
            } 
        }
        // 2. 遍历对象属性，添加属性到另一个对象中
        for(var i=0;i<state.goodsList1.length;i++){
            Object.assign(state.goodsList1[i], state.allgoodspublist[i]);
        }
        allgoods = state.commodityList.concat(state.goodsList1)
        // console.log("v",allgoods,state.commodityList,id)
        for(var i=0;i<allgoods.length;i++){
            if(allgoods[i].goodsId == id){
                goodsfrom = allgoods[i]
            }
        }
        // console.log("goodsfrom",goodsfrom)
        return goodsfrom 
    }
}

export default{
    state,
    mutations,
    actions,
    getters
}